package com.mvcplus.system.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mvcplus.system.bo.ResourceBo;
import com.mvcplus.system.mapper.SysResourceMapper;
import com.mvcplus.system.mapper.SysRoleResourceMapper;
import com.mvcplus.system.model.SysResource;
import com.mvcplus.system.model.SysRoleResource;
import com.mvcplus.system.service.ISysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SysResource 表数据服务层接口实现类
 */
@Service
public class SysResourceServiceImpl extends SuperServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {


    @Autowired
    public SysResourceMapper sysResourceMapper;

    @Autowired
    public SysRoleResourceMapper roleResourceMapper;

    //子孙节点
    private static List<SysResource> childReslist = null;

    @Override
    public List<ResourceBo> selectResourceByRole(Long id) {
        List<ResourceBo> rblist = sysResourceMapper.seletTreebyRole(id);
        return rblist;
    }

    @Override
    public int saveAuth(Long id, Long[] resIds) {
        Map<String, Object> columMap = new HashMap<String, Object>();
        columMap.put("ROLE_ID", id);
        roleResourceMapper.deleteByMap(columMap);
        List<SysRoleResource> srrlist = new ArrayList();
        if (resIds != null) {
            for (Long resId : resIds) {
                SysRoleResource srr = new SysRoleResource();
                srr.setRoleId(id);
                srr.setResId(resId);
                srrlist.add(srr);
            }
            roleResourceMapper.batchInsert(srrlist);
        }
        return 0;
    }

    @Override
    public List<SysResource> selectAllResource() {
        childReslist=new ArrayList<SysResource>();
        EntityWrapper ew = new EntityWrapper();
        ew.where("STATUS={0}", 1);
        ew.orderBy("SEQ", true);
        List<SysResource> srlist = sysResourceMapper.selectList(ew);
        if (srlist.size() > 0) {
            for (SysResource sr : srlist) {
                if (sr.getPid() == null) {
                    //根节点
                    childReslist.add(sr);
                    //根节点下面所有层级的子节点
                    List<SysResource> tmplist = treeMenuList(srlist, sr.getId());
                }
            }
        }
        return childReslist;
    }

    @Override
    public Boolean deleteAllById(Long id) {
        //删除本身（如果没有角色占用）
        System.out.println(id);
        sysResourceMapper.deleteAllbyId(id);
        //删除下面的子节点（如果没有角色占用）
        sysResourceMapper.deleteAllbyPid(id);
        return true;
    }


    /**
     * 获取某个父节点下面的所有子节点
     *
     * @param resList list
     * @param pid     父id
     * @return
     */
    public static List<SysResource> treeMenuList(List<SysResource> resList, Long pid) {
        for (SysResource r : resList) {
            //遍历出父id等于参数的id，add进子节点集合
            if (r.getPid() != null) {
                if (r.getPid().equals(pid)) {
                    //递归遍历下一级
                    childReslist.add(r);
                    treeMenuList(resList, r.getId());
                }
            }
        }
        return childReslist;
    }
}